// Author: wsfuyibing <682805@qq.com>
// Date: 2025-07-02

package operations

import (
	"server/app/http/models"
	"server/app/kind"
)

type (
	// Entity
	// 基础入参实体.
	Entity struct {
		Kind           kind.OrderKind `json:"-"`
		UseUserBalance bool           `json:",omitempty"`

		// Arcade
		// 钓场.
		Arcade        *models.Arcades       `json:",omitempty"`
		ArcadeId      kind.ArcadeId         `json:"-"`
		ArcadeMember  *models.ArcadeMember  `json:",omitempty"`
		ArcadeSetting *models.ArcadeSetting `json:",omitempty"`

		// User
		// 用户.
		User          *models.Users         `json:",omitempty"`
		UserId        kind.UserId           `json:"-"`
		UserStatistic *models.UserStatistic `json:",omitempty"`

		// Coupon
		// 优惠券.
		Coupon   *models.Coupon `json:",omitempty"`
		CouponId kind.CouponId  `json:"-"`

		// Goods
		// 商品列表.
		Goods map[kind.GoodsId]*models.Goods `json:"-"`

		// Items
		// 订单条目.
		Items []*models.OrderItem `json:",omitempty"`
	}

	// GoodsEntity
	// 购物实体.
	GoodsEntity struct {
		Entity

		// Carts
		// 购物车商品.
		Carts map[kind.GoodsId]kind.GoodsStock `json:",omitempty"`
	}

	// SessionEntity
	// 购票实体.
	SessionEntity struct {
		Entity

		// Quantity
		// 购票数量.
		Quantity kind.GoodsStock `json:"-"`

		// Session
		// 场次信息.
		Session *models.TicketSession `json:",omitempty"`

		// SessionId
		// 场次ID.
		SessionId int64 `json:"-"`

		// Ticket
		// 渔票信息.
		Ticket *models.Ticket `json:",omitempty"`
	}
)

// AddItem
// 添加条目.
func (o *Entity) AddItem(goods *models.Goods, quantity kind.GoodsStock, free bool) {
	// 1. 商品列表.
	o.Goods[goods.Id] = goods

	// 2. 商品售价.
	amount := goods.SalePrice * kind.Balance(quantity)
	reset := kind.EnabledYes

	// 3. 免费商品.
	//    购票时提供的赠品.
	if free {
		amount = amount.Zero()
		reset = kind.EnabledNo
	}

	// 4. 加入条目.
	o.Items = append(o.Items, &models.OrderItem{
		OrderKind:     kind.OrderKindGoods,
		ResourceId:    goods.Id.Int64(),
		Unit:          goods.Unit,
		Name:          goods.Name,
		Image:         goods.Image,
		Price:         goods.SalePrice,
		Quantity:      quantity,
		QuantityReset: reset,
		Amount:        amount,
	})
}

// RemoveItem
// 移除条目.
//
// 当购票时提供的赠品库存不足时, 移除赠品.
func (o *Entity) RemoveItem(goods *models.Goods) {
	var items = make([]*models.OrderItem, 0)

	// 1. 移除商品.
	if _, ok := o.Goods[goods.Id]; ok {
		delete(o.Goods, goods.Id)
	}

	// 2. 遍历条目.
	for _, item := range o.Items {
		// 2.1 忽略条目.
		//     被忽略的条目将从条目列表中去除.
		if item.OrderKind.IsGoods() && item.ToGoodsId() == goods.Id {
			continue
		}
		// 2.2 临时条目.
		items = append(items, item)
	}

	// 3. 更新条目.
	o.Items = items
}

// AddSessionItem
// 添加购票条目.
func (o *SessionEntity) AddSessionItem() {
	o.Items = append(o.Items, &models.OrderItem{
		OrderKind:     kind.OrderKindSession,
		ResourceId:    o.Session.Id,
		Unit:          kind.GoodsUnitTicket,
		Name:          o.Ticket.Name,
		Image:         o.Ticket.Image,
		Price:         o.Session.Price,
		Quantity:      o.Quantity,
		QuantityReset: kind.EnabledYes,
		Amount:        o.Session.Price * kind.Balance(o.Quantity),
	})
}
